package com.cction.activity.house

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.cction.R
import com.cction.comm.*
import com.cction.entry.HouseBean
import com.umeng.analytics.MobclickAgent
import com.zhy.adapter.recyclerview.CommonAdapter
import com.zhy.adapter.recyclerview.base.ViewHolder
import kotlinx.android.synthetic.main.recordlist_main.*
import kotlinx.android.synthetic.main.title_bar.*

/**
 * 房贷列表
 */
class HouseListActivity : AppCompatActivity(), View.OnClickListener {
    private val context = this
    private lateinit var arraylist: ArrayList<HouseBean>
    private lateinit var adapter: CommonAdapter<HouseBean>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.recordlist_main)

        init()
    }

    private fun init() {
        title_bar_title.text = "记录"
        title_bar_other.text = "清空"
        title_bar_back.setOnClickListener(this)
        title_bar_other.setOnClickListener(this)

        arraylist = ArrayList()
        record_list_recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
        adapter = object : CommonAdapter<HouseBean>(context, R.layout.record_item, arraylist) {
            override fun convert(holder: ViewHolder, houseBean: HouseBean, position: Int) {

                holder.setText(R.id.record_item_principal, "贷款金额：${Utils.keepPointDouble(houseBean.total * houseBean.mortgage)}万元")
                holder.setText(R.id.record_item_result, "月还期数：${houseBean.time * 12}月")
                holder.setText(R.id.record_item_time, "记录时间：${houseBean.ctime}")


                when (houseBean.type) {
                    1 -> {
                        holder.setText(R.id.record_item_title, "商业贷")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.orange)
                    }
                    2 -> {
                        holder.setText(R.id.record_item_title, "公积金")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.blue)
                    }
                    3 -> {
                        holder.setText(R.id.record_item_title, "组合贷")
                        holder.setBackgroundRes(R.id.record_item_layout, R.color.green)
                    }
                }
                holder.itemView.setOnClickListener {
                    val intent = Intent()
                    intent.setClass(context, HouseDetailActivity::class.java)
                    val bundle = Bundle()
                    bundle.putSerializable("time", houseBean)
                    intent.putExtras(bundle)
                    startActivity(intent)
                }
            }
        }
        record_list_recycler.adapter = adapter
    }

    override fun onClick(view: View) {
        when (view.id) {
            R.id.title_bar_back -> finish()
            R.id.title_bar_other -> {
                val builder = AlertDialog.Builder(context)
                builder.setTitle("提示")
                builder.setMessage("确定要清空所有记录吗")
                builder.setPositiveButton("取消") { _, _ -> }
                builder.setNegativeButton("确定") { _, _ ->
                    SaveHouseUtils.clear(context)
                    initData()
                }
                builder.show()
            }
        }
    }

    /**
     * 数据更新
     */
    private fun initData() {
        arraylist.clear()

        arraylist.addAll(SaveHouseUtils.readData(context).arrayList)
        if (arraylist.size == 0) {
            record_item_empty.visibility = View.VISIBLE
            title_bar_other.visibility = View.GONE
        } else {
            record_item_empty.visibility = View.GONE
            title_bar_other.visibility = View.VISIBLE
        }
        adapter.notifyDataSetChanged()
    }


    override fun onResume() {
        super.onResume()
        initData()
        MobclickAgent.onResume(this)
    }


    public override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }

}

